package com.tzxs.pay.util;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

import java.lang.ref.WeakReference;

/**
 *
 */
public class AppUtil {
    private static AppUtil appUtil;
    private WeakReference<Application> weakReference;

    private AppUtil(WeakReference<Application> weakReference) {
        this.weakReference = weakReference;
    }

    public static AppUtil getInstance(Application app) {
        if (appUtil == null) {
            synchronized (AppUtil.class) {
                if (appUtil == null) {
                    appUtil = new AppUtil(new WeakReference<>(app));
                }
            }

        }
        return appUtil;
    }

    /**
     * 获取app版本号
     *
     * @return app版本号
     */
    private int getAppVersionCode() {
        int versionCode = -1;
        PackageManager packageManager = weakReference.get().getPackageManager();
        try {
            PackageInfo info = packageManager.getPackageInfo(weakReference.get().getPackageName(), 0);
            versionCode = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(weakReference.get(), "找不到包名!", Toast.LENGTH_SHORT).show();
        }
        return versionCode;
    }



}
